gbDebug = True;
try:
  import xbmcgui;
except:
  # For command line testing outside of XBMC
  from FakeXBMCGUI import FakeXBMCGUI;
  xbmcgui = FakeXBMCGUI();

def readDisc(oSettings):
  import os, time;
  from runMakeMKVCon import runMakeMKVCon;
  from Disc import Disc;
  from getSourceDrivePathAndDeviceName import getSourceDrivePathAndDeviceName;
  oDialog = xbmcgui.Dialog();
  sDrivePath, sDeviceName = getSourceDrivePathAndDeviceName(oSettings);
  bIsMovie = not oDialog.yesno(oSettings.sName, "Is this a TV Series or a Movie?", yeslabel="TV Series", nolabel="Movie");
  if gbDebug:
    print "%s: User said disc is a %s." % (__file__, bIsMovie and "Movie" or "TV Show");
  while 1:
    try:
      oStat = os.stat(sDrivePath);
      if oStat is None:
        # Command-line will show a popup asking for a disc automatically. If cancel is pressed, None is returned.
        raise WindowsError();
    except WindowsError:
      if oDialog.yesno(oSettings.sName, "There is no disc in the drive.", "Would you like to try again?"):
        continue;
      return None;
    oTime = time.gmtime(oStat.st_mtime);
    iYear = oTime.tm_year;
    if gbDebug:
      print "%s: Disc %s was last modified in %d" % (__file__, sDrivePath, iYear);
    iMinimalDuration = oSettings.get("minimal-duration-" + (bIsMovie and "movie" or "tvshow"));
    bError, bCancelled, aasData = runMakeMKVCon(oSettings, "Reading disc content...", \
        "--minlength=%d" % (iMinimalDuration * 60), "info", "dev:%s" % sDeviceName);
    if bError or bCancelled or not aasData:
      return None;
    oDisc = Disc(sDrivePath, sDeviceName, iYear, bIsMovie);
    for asData in aasData:
      sMessageType = asData[0];
      if sMessageType == "TCOUNT":
        sMessageType, iTitleCount = asData;
      elif sMessageType == "CINFO":
        sMessageType, sInfoDescription, iLanguageId, xValue = asData;
        oDisc.addInfo(sInfoDescription, xValue);
      elif sMessageType == "TINFO":
        sMessageType, iTitleId, sInfoDescription, iLanguageId, xValue = asData;
        oTitle = oDisc.getOrAddBranch(iTitleId);
        oTitle.addInfo(sInfoDescription, xValue);
      elif sMessageType == "SINFO":
        sMessageType, iTitleId, iStreamId, sInfoDescription, iLanguageId, xValue = asData;
        oTitle = oDisc.getOrAddBranch(iTitleId);
        oStream = oTitle.getOrAddBranch(iStreamId);
        oStream.addInfo(sInfoDescription, xValue);
      else:
        print "Unknown message type %s ignored." % repr(sMessageType);
    if not oDisc.bHasContent:
      if oDialog.yesno(oSettings.sName, "No disc in drive or disc does not contain anything.", "Would you like to try again?"):
        continue;
      return None;
    if gbDebug:
      print "%s: %s" % (__file__, oDisc.sDescription);
      for sLine in oDisc.treeDump():
        print "%s:  %s" % (__file__, sLine);
    return oDisc;

if __name__ == "__main__":
  import sys;
  from FakeSettings import FakeSettings;
  
  oSettings = FakeSettings({
      "makemkvcon-path": sys.argv[1],
      "minimal-duration": int(sys.argv[2]),
  });

  from getSourceDeviceName import getSourceDeviceName;
  sDeviceName = getSourceDeviceName(oSettings);

  oDisc = readDisc(oSettings, sDeviceName);
  if oDisc and oDisc.bHasContent:
    print oDisc.sDescription;
    for sLine in oDisc.debugDump():
      print "  " + sLine;
